package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Role;
import com.qk.service.RoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class RoleController {
    private static final Logger log = LoggerFactory.getLogger(RoleController.class);
    @Autowired
    private RoleService roleService;
    /**
     * 角色列表查询
     */
    @GetMapping("/roles")
    public Result select(String name, String label, @RequestParam (defaultValue = "1") Integer page,
                         @RequestParam (defaultValue = "10") Integer pageSize){
        PageResult<Role> lists = roleService.select(name,label,page,pageSize);
        return Result.success(lists);
    }

    /**
     * 角色添加
     */
    @Log
    @PostMapping("/roles")
    public Result add(@RequestBody Role role){
        log.info("添加角色: {}",role);
        roleService.add(role);
        return Result.success();
    }

    /**
     * 根据id查询角色
     */
    @GetMapping("/roles/{id}")
    public Result getById(@PathVariable Integer id){
        Role role =roleService.getById(id);
        return Result.success(role);
    }

    /**
     * 角色修改
     */
    @Log
    @PutMapping("/roles")
    public Result update(@RequestBody Role role){
        log.info("修改角色: {}",role);
        roleService.update(role);
        return Result.success();
    }

    /**
     * 角色删除
     */
    @Log
    @DeleteMapping("/roles/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("删除角色: id: {}", id);
        roleService.delete(id);
        return Result.success();
    }

    /**
     * 获取所有角色列表
     */
    @GetMapping("/roles/list")
    public Result getAllList(){
        List<Role> roleList = roleService.getAllList();
        return Result.success(roleList);
    }
}
